<template>
	<view class="member-wrap">
		<view class="index-bg" style="height: 464rpx;"></view>
		<view class="member-head" v-if="userInfo">
			<image class="member-head-img" :src="userInfo.avatar"></image>
			<view class="member-head-text-wrap">
				<view class="member-head-name">{{ userInfo.username }}</view>
				<view class="member-head-phone">{{ userInfo.phone }}</view>
				<view class="coin-line-wrap">
					<view class="item-wrap" @click="gotoRouter('/pages-member/myDivide/myDivide')">
						<image
							src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_point_little.png">
						</image>
						<view class="item-text">积分 {{ disScore }}</view>
					</view>
					<view class="item-wrap" style="margin-left: 48rpx;"
						@click="gotoRouter('/pages-mall/myHealthCoins/myHealthCoins?busType=23')">
						<image
							src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_pocket.png">
						</image>
						<view class="item-text">我的健康金</view>
					</view>
				</view>
			</view>
			<image style="width: 44rpx;height: 44rpx;" @click="scanCode"
				src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_scan.png">
			</image>
			<!-- <view class="mine-setting-wrap">
				<image src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_setting.png">
				</image>
				<view class="mine-setting-text">设置</view>
			</view> -->
		</view>
		<!--  -->
		<view class="member-head" style="margin-bottom: 60rpx;" v-if="!userInfo" @click="wxLogin">
			<image class="member-head-img" src="http://k8s.vendingtech.vip/images/default_head.png"></image>
			<view class="member-head-text-wrap">
				<view class="member-head-name">您还未登录</view>
				<view class="member-head-phone">去登录></view>
			</view>
			<image style="width: 44rpx;height: 44rpx;" @click.stop="scanCode"
				src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_scan.png">
			</image>
		</view>

		<view class="member-main">
			<u-title value="我的订单"></u-title>
			<view class="member-main-list">
				<view class="member-main-item" @click="gotoRouter('/pages-order/inquiryOrderList/inquiryOrderList')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_diagnose_order.png">
					</image>
					<view class="main-item-info">问诊订单</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-hospital/signOrderList/signOrderList')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_sign_order.png">
					</image>
					<view class="main-item-info">我的签约</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-order/drugOrderList/drugOrderList')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_medicine_order.png">
					</image>
					<view class="main-item-info">药品订单</view>
				</view>
			</view>
		</view>

		<view class="member-main">
			<u-title value="医疗服务"></u-title>
			<view class="member-main-list">
				<!-- <view class="member-main-item" @click="gotoRouter('/pages-order/inquiryOrderList/inquiryOrderList')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_reserve.png">
					</image>
					<view class="main-item-info">我的预约</view>
				</view> -->
				<view class="member-main-item" @click="gotoRouter('/pages-hospital/myDoctors/myDoctors')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_doctor.png">
					</image>
					<view class="main-item-info">我的医生</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-order/prescriptionList/prescriptionList')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_recipe.png">
					</image>
					<view class="main-item-info">我的处方</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-member/myFollow/myFollow')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_visit.png">
					</image>
					<view class="main-item-info">我的随访</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-order/recordList/recordList')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_recipe.png">
					</image>
					<view class="main-item-info">我的病历</view>
				</view>
			</view>
		</view>

		<view class="member-main">
			<u-title value="常规应用"></u-title>
			<view class="member-main-list">
				<view class="member-main-item" @click="gotoRouter('/pages-mall/coupons/coupons')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_coupon.png">
					</image>
					<view class="main-item-info">我的优惠券</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-member/evaluateList/evaluateList?type=1')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_comment.png">
					</image>
					<view class="main-item-info">我的评论</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-member/myDivide/myDivide')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_point.png">
					</image>
					<view class="main-item-info">我的积分</view>
				</view>
				<view class="member-main-item" @click="showOrHideQRCode()">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_share.png">
					</image>
					<view class="main-item-info">分享好友</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-member/patientList/patientList')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_user.png">
					</image>
					<view class="main-item-info">就诊人管理</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-member/addressList/addressList')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_address.png">
					</image>
					<view class="main-item-info">地址管理</view>
				</view>
				<button class="member-main-item" open-type="contact" bindcontact="handleContact">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_feedback.png">
					</image>
					<view class="main-item-info">投诉建议</view>
				</button>
				<button class="member-main-item" open-type="contact" bindcontact="handleContact">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_mine_server.png">
					</image>
					<view class="main-item-info">客服中心</view>
				</button>
			</view>
		</view>
<!-- 		onIdentity
		identitySwitch -->
		<view class="member-main">
			<u-title value="管理端"></u-title>
			<view class="member-main-list">
				<view class="member-main-item" @click="onIdentity(1)">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_client_doctor.png">
					</image>
					<view class="main-item-info">医师入口</view>
				</view>
				<view class="member-main-item" @click="onIdentity(2)">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_client_pharmacist.png">
					</image>
					<view class="main-item-info">药师入口</view>
				</view>
				<view class="member-main-item" @click="onIdentity(3)">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/mine/ic_client_orgnization.png">
					</image>
					<view class="main-item-info">机构入口</view>
				</view>
			</view>
		</view>

		<view v-if="userInfo" class="logout-btn" @click="logout()">退出登录</view>

		<!-- <image class="member-ikon" src="http://k8s.vendingtech.vip/images/banner.png"></image> -->
		<view v-if="false" class="member-ikon">
			<u-banner></u-banner>
		</view>
		<view v-if="qrcodeShow" class="qrcode-root">
			<view class="qrcode-dialog">
				<image class="close-btn"
					src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ic_float_close.png"
					@click="showOrHideQRCode()"></image>
				<view class="poster-wrap">
					<l-painter ref="painter">
						<l-painter-view
							css="width: 568rpx; box-sizing:border-box; background: #ffffff; align-items: center; padding: 24rpx; border-radius: 24rpx;">
							<l-painter-image
								src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ikon@2x.png"
								css="width: 480rpx; height: 332rpx; margin: 24rpx auto; border-radius: 24rpx;" />
							<l-painter-text css="font-size: 32rpx; color: #666666; margin: 24rpx 0"
								text="邀请您使用在线问诊平台"></l-painter-text>
							<l-painter-view
								css="display:block; height: 2rpx; background: #E5E5E5; margin: 0;"></l-painter-view>
							<l-painter-view css="display:flex; align-items:center; margin: 24rpx;">
								<l-painter-image
									css="width: 88rpx; height: 88rpx; border-radius: 44rpx; margin-right: 16rpx"
									:src="userInfo.avatar"></l-painter-image>
								<l-painter-view css="flex:1; box-sizing:border-box;">
									<l-painter-text
										css="font-size: 32rpx; color: #222222; font-weight: 500; display:block;"
										:text="userInfo.username"></l-painter-text>
									<l-painter-text css="font-size: 24rpx; color: #999999;"
										text="长按识别二维码"></l-painter-text>
								</l-painter-view>
								<l-painter-qrcode :text="qrcodeContent"
									css="width: 120rpx;height: 120rpx;"></l-painter-qrcode>
							</l-painter-view>
						</l-painter-view>
					</l-painter>
				</view>
				<view class="btn-line">
					<button class="confirm-btn" open-type="share">微信分享</button>
					<view class="confirm-btn" @click="saveQRCode()">保存至相册</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		startEid
	} from '@/mp_ecard_sdk/main'

	import uTitle from '@/components/u-title/u-title.vue';
	import uBanner from '@/components/u-banner/u-banner.vue';
	import {
		imInfo,
		doctorInfo,
		currentImInfo,
		token,
		userInfo
	} from '@/utils/token.js';
	const app = getApp();
	export default {
		components: {
			uTitle,
			uBanner
		},
		data() {
			return {
				userInfo: null,
				qrcodeContent: '',
				qrcodeShow: false,
				disScore: 0
			};
		},
		onLoad(option) {},
		onShow(options) {
			clearInterval(this.time);
			this.refreshPoint();
			this.userInfo = userInfo.get();
			if (this.userInfo) {
				this.qrcodeContent = 'https://disugroup.com/qrcode?scene=' + 'user_invite&id=' + this.userInfo.id;
			}
			this.time = setInterval(() => {
				if (!this.userInfo) {
					this.userInfo = userInfo.get();
					if (this.userInfo) {
						this.qrcodeContent = 'https://disugroup.com/qrcode?scene=' + 'user_invite&id=' +
							this.userInfo.id;
					}
				}
			}, 1000);
		},
		onShareAppMessage(res) {
			if (res.from === 'button') {
				return {
					title: '邀请您使用在线问诊平台',
					path: this.qrcodeContent.replace('https://disugroup.com/qrcode',
						'/pages-mall/qrcodeHandler/qrcodeHandler'),
					imageUrl: 'https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ikon@2x.png'
				}
			}
		},
		methods: {
			handleContact(e) {},
			onIdentity(roleType) {
				this.$api.identity({
					openId: this.userInfo.openId
				}).then(res1 => {
					if (res1.code == 200) {
						if (res1.result == 1 || res1.result == 2) {
							if (res1.result == roleType) {
								this.onFace(roleType)
							} else {
								this.identitySwitch(roleType);
							}
						} else if (res1.result == 3) {
							this.onFace(roleType)
						} else {
							this.identitySwitch(roleType);
						}
					} else {
						this.identitySwitch(roleType);
					}
				});
			},
			onFace(roleType) {
				let _this = this
				this.$api.detectAuth({
					openId: this.userInfo.openId
				}).then(res1 => {
					startEid({
						data: {
							token: res1.result.eidToken,
						},
						verifyDoneCallback(res2) {
							const {
								token,
								verifyDone
							} = res2;

							_this.$api.getUserDetail({}).then(useres => {
								if (useres.code == 200) {
									_this.userInfo = useres.result;
									_this.$api.detectAuthResult({
										eidToken: token,
										openId: _this.userInfo.openId
									}).then(res3 => {
										if (res3.result) {
											_this.identitySwitch(roleType);
										} else {
											uni.showModal({
												title: '提示',
												content: '身份匹配失败',
												showCancel: false,
												success: res => {}
											})
										}
									});
								}
							});
						},
					});
				});
			},
			//切换角色
			identitySwitch(roleType) {
				this.$api.identitySwitch({
						params: {
							role: roleType
						}
					})
					.then(res => {
						if (roleType == 3) {
							if (res.code == 200) {
								this.getAgencyInfo();
								// this.gotoRouter('/pages-agencies/agencies/agencies');
							} else {
								this.$api.getAgenciesState().then(e => {
									const {
										code,
										result
									} = e
									if (code == 200) {
										if (result.code == -1) {
											//跳转申请页面
											this.gotoRouter('/pages-agencies/settled/settled');
										}
										if (result.code == 0) {
											this.gotoRouter('/pages-agencies/auditDuring/auditDuring');
										}
										if (result.code == 2) {
											this.gotoRouter('/pages-agencies/auditRefused/auditRefused');
										}
									}
								})
							}
						} else {
							if (res.code == 200) {
								this.getDoctorInfo(roleType);
							} else {
								//跳转申请页面
								this.gotoRouter('/pages-doctor/auth/auth?doctorType=' + roleType);
							}
						}
					});
			},

			confirmSwitch(roleType, sdkAppId, id, userSig) {
				if (roleType == 0) {
					app.globalData.role = 0;
					currentImInfo.set({
						sdkAppId: imInfo.get().sdkAppId,
						id: imInfo.get().id,
						userSig: imInfo.get().userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					getApp().globalData.txyImController.logout();
					getApp().globalData.txyImController.login(imInfo.get().id, imInfo.get().userSig);
					uni.reLaunch({
						url: '/pages/index/index'
					});
				} else if (roleType == 1) {
					app.globalData.role = 1;
					currentImInfo.set({
						sdkAppId,
						id,
						userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					getApp().globalData.txyImController.logout();
					getApp().globalData.txyImController.login(id, userSig);
					uni.reLaunch({
						url: '/pages/doctor/doctor'
					});
				} else if (roleType == 2) {
					app.globalData.role = 2;
					currentImInfo.set({
						sdkAppId,
						id,
						userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					getApp().globalData.txyImController.logout();
					getApp().globalData.txyImController.login(id, userSig);
					uni.reLaunch({
						url: '/pages-doctor/pharmacist/pharmacist'
					});
				} else {
					app.globalData.role = 3;
					// currentImInfo.set({
					// 	sdkAppId,
					// 	id,
					// 	userSig
					// }, Date.parse(new Date()) + (86400000 * 29));
					// getApp().globalData.txyImController.logout();
					// getApp().globalData.txyImController.login(id, userSig);
					uni.reLaunch({
						url: '/pages-agencies/agencies/agencies'
					});
				}
			},

			refreshPoint() {
				this.$api.getUserPoint().then(res => {
					if (res.code == 200) {
						this.disScore = res.result.disScore;
					}
				})
			},

			logout() {
				// wx.startVerify({
				//     data: {
				//       token: 'zxczxcdaasdasdadadasdad', // 必要参数，BizToken
				//       startPath: 'pages/verify/index' // 必要参数，配置了verify核身组件的页面地址
				//     },
				//     success: (res) => { // 验证成功后触发
				//       // res 包含验证成功的token, 这里需要加500ms延时，防止iOS下不执行后面的逻辑
				//       // 验证成功后，拿到token后的逻辑处理，具体以客户自身逻辑为准
				//     },
				//     fail: (err) => {  // 验证失败时触发
				//       // err 包含错误码，错误信息，弹窗提示错误
				//     }
				//   });
				// return
				uni.showModal({
					title: '确认要退出登录吗？',
					success: res => {
						if (res.confirm) {
							this.$api.logout({}).then(res => {
								uni.showToast({
									title: '登出成功',
									icon: 'none'
								})
								userInfo.clear();
								token.clear();
								this.userInfo = null;
								getApp().globalData.txyImController.logout();
								// uni.reLaunch({
								// 	url:'/pages/index/index'
								// })
							})
						}
					}
				});
			},

			getUserDetail() {
				this.$api.getUserDetail({}).then(res => {
					if (res.code == 200) {
						this.userInfo = res.result;
					}
				});
			},

			//获取医生/药师个人信息
			getDoctorInfo(roleType) {
				this.$api.getDoctorInfo({}).then(res => {
					if (roleType != 0 && res.code == 200 && res.result != null) {
						// this.confirmSwitch(res.result?.imToken, res.result?.id);
						doctorInfo.set(res.result, Date.parse(new Date()) + (86400000 * 29))
						// 获取医生/药师的腾讯云IM信息
						this.$api.doctorTxyImInfo({
							params: {
								id: res.result.id
							}
						}).then(res_ => {
							this.confirmSwitch(roleType, res_.result.sdkAppId, res_.result.id, res_.result
								.userSig);
						})
					} else if (roleType == 0) {
						this.confirmSwitch(0);
					} else {
						uni.showToast({
							title: '身份切换失败',
							icon: 'none'
						});
					}
				});
			},

			getAgencyInfo() {
				this.confirmSwitch(3);
			},

			// 扫码
			scanCode() {
				var that = this;
				uni.scanCode({
					success: function(res) {
						uni.navigateTo({
							url: res.result.replace('https://disugroup.com/qrcode',
								'/pages-mall/qrcodeHandler/qrcodeHandler')
						})
						// that.gotoRouter();
					}
				});
			},

			showOrHideQRCode() {
				this.qrcodeShow = !this.qrcodeShow;
			},

			saveQRCode() {
				uni.showLoading({
					title: '保存中',
					mask: true
				});
				this.$refs.painter.canvasToTempFilePath({
					fileType: "jpg",
					// 如果返回的是base64是无法使用 saveImageToPhotosAlbum，需要设置 pathType为url
					pathType: 'url',
					quality: 1,
					success: (res) => {
						uni.showToast({
							icon: 'success',
							title: '保存成功'
						});
						console.log(res.tempFilePath);
						// 非H5 保存到相册
						// H5 提示用户长按图另存
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: function() {
								console.log('save success');
							}
						});
					},
					complete: () => {
						uni.hideLoading();
					}
				});

			}
		}
	};
</script>

<style lang="less">
	@import './member.less';
</style>